package com.daisuxia.risk.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.daisuxia.back.utils.ServiceResult;
import com.daisuxia.risk.dao.IRiskCreditUserDao;
import com.daisuxia.risk.pojo.RiskCreditUser;
import com.daisuxia.risk.utils.ConstantRisk;
import com.daisuxia.risk.utils.RiskCreditUserUtil;
import com.daisuxia.risk.utils.ThreadPool;

import redis.clients.jedis.JedisCluster;

@Component("riskTaskJob")
public class RiskTaskService implements IRiskTaskService {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private IJxlService jxlService;
	@Autowired
	private IRiskCreditUserDao riskCreditUserDao;
	@Autowired
	private IRiskCreditUserService riskCreditUserService;
	@Autowired
	JedisCluster jedisCluster;

	@Override
	public void findJxlReport() {
		logger.info("findJxlReport is running");
		List<HashMap<String, Object>> list = riskCreditUserDao
				.findJxlWaitReport();
		if (list != null && list.size() > 0) {
			for (final HashMap<String, Object> map : list) {
				try {
					String value = map.get("userId") + "";
					final String key = ConstantRisk.JXL_REPORT + value;
					if (jedisCluster.get(key) != null) {
						logger.info("others is findJxlReport key=" + key);
					} else {
						jedisCluster
								.setex(key, ConstantRisk.FLAG_SECOND, value);
						ThreadPool.getInstance().run(new Runnable() {
							@Override
							public void run() {
								HashMap<String, Object> map1 = new HashMap<String, Object>();
								map1.putAll(map);
								ServiceResult serviceResult = jxlService
										.findUserReport(map1);
								if (serviceResult.isSuccessed()) {
									map1.put("detail", serviceResult.getMsg());
									RiskCreditUser riskCreditUser3 = RiskCreditUserUtil
											.getInstance().createJxl(map1);
									if (riskCreditUser3 != null) {
										riskCreditUserService
												.updateJxl(riskCreditUser3);
									} else {
										logger
												.info("updateJxl error riskCreditUser3="
														+ riskCreditUser3);
									}
								}
								jedisCluster.del(key);
							}
						});
					}

				} catch (Exception e) {
					logger.error("findJxlReport error map=" + map, e);
				}
			}
		}
	}


	@Override
	public void updateAnalysis() {
		logger.info("updateAnalysis is running");
		List<HashMap<String, Object>> list = riskCreditUserDao
				.findWaitAnalysis();
		if (list != null && list.size() > 0) {
			for (final HashMap<String, Object> params : list) {
				try {
					String value = params.get("assetId") + "";
					final String key = ConstantRisk.REVIEW_BORROW + value;
					if (jedisCluster.get(key) != null) {
						logger.info("others is reviews key=" + key);
					} else {
						jedisCluster
								.setex(key, ConstantRisk.FLAG_SECOND, value);
						ThreadPool.getInstance().run(new Runnable() {
							@Override
							public void run() {
								riskCreditUserService.updateAll(params);
								jedisCluster.del(key);
							}
						});
					}
				} catch (Exception e) {
					logger.error("updateAnalysis error params=" + params, e);
				}

			}
		}
	}

	@Override
	public void updateEd() {
		logger.info("updateEd is running");
		List<RiskCreditUser> list = riskCreditUserDao.findCalMoney();
		if (list != null && list.size() > 0) {
			for (final RiskCreditUser riskCreditUser : list) {
				try {
					String value = riskCreditUser.getUserId() + "";
					final String key = ConstantRisk.USER_MONEY + value;
					if (jedisCluster.get(key) != null) {
						logger.info("others is updateEd key=" + key);
					} else {
						jedisCluster
								.setex(key, ConstantRisk.FLAG_SECOND, value);
						ThreadPool.getInstance().run(new Runnable() {
							@Override
							public void run() {
								riskCreditUserService
										.updateBorrowMoney(riskCreditUser);
								jedisCluster.del(key);
							}
						});
					}

				} catch (Exception e) {
					logger.error("updateEd error riskCreditUser="
							+ riskCreditUser, e);
				}
			}
		}
	}
}
